<?
//Cart 0.0.1
class sym_cart extends symbiont{
    public function ___main($design=''){
        global $user, $kernel, $sql, $sym;
        if($user->access_level<1){
            $ids=array();
            $products=array();
            for($i=0; isset($_COOKIE['product_'.$i]);$i++){
                $id=data::number($_COOKIE['product_'.$i]);
                $r=$sql->query_each("
                                    SELECT prod.id, prod.price, prod.name, prod.currency, c.name as category_name, 1 as count, p.title
                                    FROM `sym_product` as prod
                                    LEFT JOIN `paragraphs` as p
                                        ON prod.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                    LEFT JOIN `sym_category` as c
                                        ON c.id=prod.category_id
                                    WHERE prod.id=".$id."
                                    ", true);
                if($r) $products[]=$r;
            }
        }
        else{
            $products=$sql->query_each("
                                    SELECT prod.id, prod.price, prod.name, prod.currency, c.name as category_name, cart.count, p.title
                                    FROM `sym_cart` as cart
                                        LEFT JOIN `sym_product` as prod
                                            ON cart.product_id=prod.id
                                        LEFT JOIN `paragraphs` as p
                                            ON prod.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                        LEFT JOIN `sym_category` as c
                                            ON c.id=prod.category_id
                                    WHERE cart.user_id=".$user->id." AND cart.status=0
                                    ORDER BY cart.id
                                       ");
        }
        
        ob_start();
        $kernel->symbiont_add('rate');
        $sym->rate->___main('json');
        $file=ob_get_clean();
        $json=json_decode($file);
        
        $design=$this->_check($design, 'main');
        $d=new design($design, 'cart', false);
        $this->items='';
        if(is_array($products)){
            foreach($products as $val){
                $this->id=$val['id'];
                if($val['currency']!='amd'){
                    $cur=$val['currency'].'_buy';
                    $this->price=round($json->$cur*$val['price']/10, 0)*10;
                }
                else{
                    $this->price=$val['price'];
                }
                $this->title=$val['title'];
                $this->count=$val['count'];
                $this->name=$val['name'];
                $this->category_name=$val['category_name'];
                $this->items.=design::run($this->item);
            }
        }
        $d->inc();
    }
    public function ___db_add($design='', $product_id=''){
        global $sql, $user;
        if(isset($_POST['product_id'])){
            if(!data::is_uint($_POST['product_id'])){
                $l=new label('error.type');
                return('{"error":"'.$l->get().'"}');
            }
            $product_id=$_POST['product_id'];
        }
        if(!$product_id){
            $l=new label('error.parametrs');
            return('{"error":"'.$l->get().'"}');
        }
        if($user->access_level<1){
            $t=true;
            for($i=0; isset($_COOKIE['product_'.$i]);$i++){
                $id=$_COOKIE['product_'.$i];
                if($id==$product_id){
                    $t=false;
                    break;
                }
            }
            if($t){
                setcookie('product_'.$i, $product_id, time()+604800, '/');
                $l=new label('symbionts.cart.added');
                return('{"ok":"'.$l->get().'", "add":"1"}');
            }
            else{
                $l=new label('error.other');
                return('{"error":"'.$l->get().'"}');
            }
        }
        else{
            $count=$sql->select_each('sym_cart', 'count', array('user_id'=>$user->id, 'product_id'=>$product_id, 'status'=>0), '', '', 1);
            if($count){
                $id=$sql->update('sym_cart', array('count'=>$count+1), array('user_id'=>$user->id, 'product_id'=>$product_id));
                $add=false;
            }
            else{
                $id=$sql->insert('sym_cart', array('user_id'=>$user->id, 'product_id'=>$product_id));
                $add=true;
            }
            if($id){
                $l=new label('symbionts.cart.added');
                return('{"ok":"'.$l->get().'", "add":"'.$add.'"}');
            }
            else{
                $l=new label('error.other');
                return('{"error":"'.$l->get().'"}');
            }
        }
    }
    public function ___order($design=''){
        global $user, $kernel, $sym, $sql;
        if($user->access_level<1){
            $kernel->symbiont_add('user');
            $sym->user->___signin('');
            $sym->user->___signup('signup_order');
        }
        elseif($user->access_level==1){
            $kernel->symbiont_add('user');
            $sym->user->___profile('edit');
        }
        else{
            $this->___main('order');
            /*
            $id=$sql->update('sym_cart', array('status'=>1), array('user_id'=>$user->id, 'status'=>0));
            if($id){
                $l=new label('symbionts.cart.order_complite');
                print $l->get();
            }
            else{
                $l=new label('symbionts.cart.order_error');
                print $l->get();
            }
            */
        }
    }
    public function _administrate($design=''){
        $design=$this->_check($design, 'administrate');
        $this->options='';
        $d=new design($design, 'cart', false);
        
        $this->name='order';
        $this->title='Order';
        $this->options.=design::run($this->option);
        
        $d->inc();
    }
}
?>